package com.tong.lib.base;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * Created by hjt on 2019/7/27.
 */

public abstract class LazyFragment extends BaseFragment {

    private boolean isInitView;
    private boolean isLoad;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            if (isDataBinding()){
                initDataBinding(inflater,getLayoutResID(),container,savedInstanceState);
            }else {
                mView = inflater.inflate(getLayoutResID(), container, false);
                mUnbinder = ButterKnife.bind(this, mView);
            }
            init(savedInstanceState);
            initEvent();
            isInitView = true;
            isLoadData();
        }
        return mView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isLoadData();
    }

    private void isLoadData() {
        if (getUserVisibleHint() && isInitView && !isLoad) {
            isLoad = true;
            lazyLoad();
        }
    }

    protected void initDataBinding(LayoutInflater inflater,int layoutRes,ViewGroup container,Bundle savedInstanceState) {

    }

    protected abstract void lazyLoad();

    protected boolean isDataBinding(){
        return false;
    }
}
